<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Product extends Controller_Default {

  public function action_index()
  {
    $this->template->title = 'Продкты-СтройТех';
    $this->template->content = View::factory('product/index');
  }

  public function action_getajax()
  {
    if ($this->request->is_ajax())
    {
      $category = ORM::factory('Category', $this->request->post('category_id'))->as_array();
      $products = ORM::factory('Product')->where('category_id', '=', $this->request->post('category_id'))->find_all();
      $products_array = array();
      foreach ($products as $product) {
        $product->description = Text::limit_words($product->description, 10);
        $products_array[] = $product->as_array();
      }
      $this->request->headers('Content-type','application/json; charset='.Kohana::$charset);
      $this->response->body(json_encode(array('products' => $products_array, 'category' => $category)));
    }
  }

  public function action_view()
  {
    $product_id = $this->request->param('id');
    $product = ORM::factory('Product', $product_id);
    $this->template->title = $product->title . '-СтройТех';
    $this->template->content = View::factory('product/view', array('product' => $product));
//    print_die($product->as_array());
  }

}